package com.example.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.config.ResponseVO;
import com.example.pojo.DishesInfo;
import com.example.pojo.EnrollInfo;
import com.example.pojo.RecipeDate;
import com.example.pojo.RecipeInfo;
import com.example.pojo.dto.DishRecipeDTO;
import com.example.pojo.dto.DishWithAllergyInfo;
import com.example.service.DishesService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import static net.sf.jsqlparser.parser.feature.Feature.limit;

@RestController
@RequestMapping("/dishes")
public class DishesController {
    @Resource
    private DishesService dishesService;

    // 查询所有的菜品
    @GetMapping("/getAllDishes")
    public ResponseEntity<ResponseVO<List<DishesInfo>>> getAllDishes(){
        return dishesService.selectAllDishes();
    }

    //分页
    @GetMapping("/dishesInfoListPage/{currentPage}/{limit}/{dishesName}")
    public ResponseEntity<ResponseVO<IPage<DishesInfo>>> getDishesByName(@PathVariable int currentPage, @PathVariable int limit,@PathVariable String dishesName) {
        IPage<DishesInfo>  enrollInfoIPage = dishesService.getDishesByName(currentPage, limit, dishesName);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户信息获取成功", enrollInfoIPage));
    }
    @GetMapping("/dishesInfoListPage/{currentPage}/{limit}")
    public ResponseEntity<ResponseVO<IPage<DishesInfo>>> getAllDishesPage(@PathVariable int currentPage, @PathVariable int limit) {
        IPage<DishesInfo>  dishesInfoIPage = dishesService.getAllDishesPage(currentPage, limit);
        return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK, "用户信息获取成功", dishesInfoIPage));
    }

    //批量删除
    @DeleteMapping("/batchDeleteDishesInfo")
    public ResponseEntity<ResponseVO<String>> batchDeleteDishesInfo(@RequestParam List<Integer> dishesInfoArr){
        try {
            int result = dishesService.batchDeleteDishesInfo(dishesInfoArr);
            if(result>0){
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.OK,"删除成功"));
            }else{
                return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败,请稍后再试"));
            }
        } catch (Exception e) {
            return ResponseEntity.ok(new ResponseVO<>(HttpStatus.BAD_REQUEST,"删除用户失败 " + e.getMessage()));
        }
    }

    // 添加和修改菜品
    @PostMapping("/addOrUpdateDish")
    public ResponseEntity<ResponseVO<String>> addOrUpdateDish(@RequestBody DishesInfo dishesInfo){
        return dishesService.addOrUpdateDish(dishesInfo);
    }
    // 删除菜品
    @DeleteMapping("/deleteDish/{dishesInfoId}")
    public ResponseEntity<ResponseVO<String>> deleteDish(@PathVariable Integer dishesInfoId){
        return dishesService.deleteDish(dishesInfoId);
    }

    // 新增食谱
    @PostMapping("/addOrUpdateRecipeInfo")
    public ResponseEntity<ResponseVO<String>> addOrUpdateRecipeInfo(@RequestBody RecipeInfo recipeInfo){
        return dishesService.addOrUpdateRecipeInfo(recipeInfo);
    }
    // 根据食谱id查询
    @PostMapping("/getDishesByRecipeId/{recipeId}")
    public ResponseEntity<ResponseVO<List<DishesInfo>>> getDishesByRecipeId(@PathVariable Integer recipeId){
        return dishesService.getDishesByRecipeId(recipeId);
    }

    // 添加日期的食谱
    @PostMapping("/addOrUpdateRecipeDate")
    public ResponseEntity<ResponseVO<String>> addOrUpdateRecipeDate(@RequestBody RecipeDate recipeDate){
        return dishesService.addOrUpdateRecipeDate(recipeDate);
    }

    // 根据日期和班级查询食谱
    @PostMapping("/getRecipeByDateOrClassId/{date}/{classId}")
    public ResponseEntity<ResponseVO<List<DishWithAllergyInfo>>> getRecipeByDateOrClassId(@PathVariable String date, @PathVariable Long classId){
        return dishesService.getRecipeByDateOrClassId(date,classId);
    }

    // 根据日期查询食谱
    @PostMapping("/getRecipeByDate/{date}")
    public ResponseEntity<ResponseVO<List<DishesInfo>>> getRecipeByDate(@PathVariable String date){
        return dishesService.getRecipeByDate(date);
    }

    // 获取菜品加食谱全部的数据
    @PostMapping("/getDishRecipeDtoList")
    public ResponseEntity<ResponseVO<List<DishRecipeDTO>>> getDishRecipeDtoList(){
        return dishesService.getDishRecipeDtoList();
    }
}
